package com.jjw.springbootcvs.controller;


import com.github.pagehelper.PageInfo;
import com.jjw.springbootcvs.config.BootCvsProperties;
import com.jjw.springbootcvs.pojo.dto.SysUserAndSysRoleDTO;
import com.jjw.springbootcvs.pojo.entity.SysRole;
import com.jjw.springbootcvs.pojo.entity.SysUser;
import com.jjw.springbootcvs.pojo.vo.PageUserListVO;
import com.jjw.springbootcvs.service.SysRoleService;
import com.jjw.springbootcvs.service.UserService;
import com.jjw.springbootcvs.utils.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author xiaojuan
 * @Date 2024/11/1
 * @Description user控制器
 */
@Controller
@RequestMapping("/sys")
@RequiredArgsConstructor
public class UserController {

    private  final  UserService userService;

    private final SysRoleService sysRoleService;

    private final BootCvsProperties bootCvsProperties;

    /**
     * 用户列表查询
     * @param sysUserAndSysRoleDTO
     * @param model
     * @return
     */
    @GetMapping("/user/list")
    public String list(SysUserAndSysRoleDTO sysUserAndSysRoleDTO, Model model) {
       List<SysRole> sysRole =  sysRoleService.getAllRoleList();
      PageInfo<PageUserListVO> userList = userService.getAllUserList(sysUserAndSysRoleDTO);
        model.addAttribute("roleList", sysRole)
                .addAttribute("userList",userList.getList())
                .addAttribute("totalCount",userList.getTotal())
                .addAttribute("currentPageNo",userList.getPageNum())
                .addAttribute("totalPageCount",userList.getPages());
        return "/sysUser/list";
    }

    /**
     * 判断用户是否存在
     * @param code
     * @return
     */
    @GetMapping("/user/userExist")
    @ResponseBody
    public Result getUserByAccount(@RequestParam("account") String code){
        SysUser user = userService.getUserByAccount(code);
        return Result.success();
    }

    /**
     * 查询角色
     * @param model
     * @returna
     */
    @GetMapping("/role/simpleList")
    @ResponseBody
    public Result getRoleList(Model model){
        List<SysRole> sysRoleList = sysRoleService.getAllRoleList();
        return Result.success(sysRoleList);
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @PostMapping("/user/add")
    public String add(SysUser sysUser, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        sysUser.setUpdatedUserId(user.getId());
        sysUser.setCreatedUserId(user.getId());
        userService.insert(sysUser);
        // 重定向到用户列表页面
        return "redirect:/sys/user/list";
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/user/del/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Long id){
        userService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查询用户信息给修改表单赋值
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/user/toUpdate")
    public String toUpdate(@RequestParam("uid") Long id, Model model){
        SysUser user = userService.getUserById(id);
        List<SysRole> sysRoleList = sysRoleService.getAllRoleList();
        model.addAttribute("sysUser",user);
        model.addAttribute("roleList",sysRoleList);
        return "/sysUser/update";
    }

    /**
     * 修改用户
     * @param sysUser
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping("/user/update")
    public String update(SysUser sysUser, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        sysUser.setUpdatedUserId(user.getId());
        userService.update(sysUser);
        // 重定向到用户列表页面
        return "redirect:/sys/user/list";
    }

    /**
     * 查看用户
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/user/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        SysUser user = userService.getUserById(id);
        model.addAttribute("sysUser",user);
        return "/sysUser/view";
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/user/savePassword")
    public String savePassword(HttpSession session,@RequestParam("newPassword") String newPassword,@RequestParam("oldPassword") String oldPassword,@RequestParam("reNewPassword")String reNewPassword){
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        userService.updateByPassword(user.getId(), newPassword);
        session.removeAttribute(bootCvsProperties.getSessionKey());
        return "/login";
    }

    /**
     * 校验旧密码
     * @param oldPassword
     * @param session
     * @return
     */
    @PostMapping("/user/checkOldPwd")
    @ResponseBody
    public Result checkOldPwd(@RequestParam("oldPassword") String oldPassword, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        SysUser sysUser = userService.getUserByPassword(user.getId(), oldPassword);
        return Result.success();
    }
}
